<?php
class erpapi_unionpay_response_process_logistics {
    static public $sms_method = array(
        '2'=>'express',#已揽收
        '1'=>'received',#已签收
    );

    public function push($params) {
        $filter = array(
            'delivery_id' => $params['delivery_id'],
            'is_received|noequal' => $params['is_received']
        );
        $strReceived = $params['is_received'] == '1' ? '签收' : ($params['is_received'] == '2' ? '揽收' : '');
        $ret = app::get('ome')->model('delivery')->update(array('is_received'=>$params['is_received']), $filter);
        if(is_bool($ret)) {
            return array('rsp'=>'succ', 'msg'=>'该发货单已' . $strReceived);
        }
        $memo = '更新签收状态为：已' . $strReceived . '（物流单号：'.$params['logi_no'].'）';
        app::get('ome')->model('operation_log')->write_log('delivery_process@ome',$params['delivery_id'],$memo);
        $method = self::$sms_method[$params['is_received']];
        #已揽收、已签收的，需要发送短信
        if($method){
            #如果taoexlib存在，发货短信开启的 发送短信
            if(kernel::service('message_setting') && defined('APP_TOKEN') && defined('APP_SOURCE')){
                kernel::single('taoexlib_sms')->sendSms(array('event_type'=>$method,'delivery_id'=>$params['delivery_id']), $error_msg);

            }
        }
        #只有已签收的货到付款单才自动支付
        if($params['is_received'] == '1' && $params['is_cod'] == 'true' && 'false' != app::get('ome')->getConf('ome.codorder.autopay')){
            $this->_codAutoPay($params['delivery_id']);
        }
        return array('rsp'=>'succ', 'msg' => '更新物流状态成功!');
    }

    private function _codAutoPay($deliveryId) {
        $objOrder = app::get('ome')->model('orders');
        $oPayment = app::get('ome')->model('payments');
        $orderDelivery = app::get('ome')->model('delivery_order')->getList('*',array('delivery_id'=>$deliveryId));
        $arrOrderId = array();
        foreach($orderDelivery as $od) {
            $arrOrderId[] = $od['order_id'];
        }
        $filter = array('order_id'=>$arrOrderId,'is_cod'=>'true','pay_status'=>'0');
        $arrOrderData = $objOrder->getList('order_id,total_amount,ship_status,shop_id,currency,source', $filter);
        $arrShopId = array();
        foreach($arrOrderData as $val) {
            $arrShopId[$val['shop_id']] = $val['shop_id'];
        }
        $shop = app::get('ome')->model('shop')->getList('shop_id, node_id, node_type', array('shop_id'=>$arrShopId));
        $arrShopData = array();
        foreach($shop as $val) {
            $arrShopData[$val['shop_id']] = $val;
        }
        $objsales = app::get('ome')->model('sales');
        $saleData = $objsales->getList('sale_id,order_id', array('order_id' => $arrOrderId));
        $orderSale = array();
        foreach($saleData as $sale) {
            $orderSale[$sale['order_id']][] = $sale['sale_id'];
        }
        $paymentCfg =  app::get('ome')->model('payment_cfg')->getList('id', array('pay_bn'=>'offline'), 0, 1);
        $c2c_shop = ome_shop_type::shop_list();
        foreach ($arrOrderData as $order) {
            $shop_detail = $arrShopData[$order['shop_id']];
            if ($shop_detail['node_id'] && !in_array($shop_detail['node_type'],$c2c_shop) && $order['source'] == 'matrix'){
                $params = array (
                    'order_id' => $order['order_id'],
                    'shop_id' => $order['shop_id'],
                    'money' => $order['total_amount'],
                    'pay_type' => 'offline',
                    'payment' => intval($paymentCfg[0]['id']),
                    'memo' => '签收COD订单系统自动确认支付',
                );
                $oPayment->payment_request($params);
                continue;
            }
            $orderData = array();
            $orderData['payed']      = $order['total_amount'];
            $orderData['pay_status'] = 1;
            $orderData['paytime']    = time();
            $orderData['payment']    = '线下支付';
            if($order['ship_status'] == 1) {
                $orderData['status'] = 'finish';
                if($orderSale[$order['order_id']]) {
                    $objsales->update(array('paytime' => $orderData['paytime']), array('order_id' => $order['order_id']));
                }
            }
            $orderFilter = array('order_id'=>$order['order_id']);
            $objOrder->update($orderData, $orderFilter);
            //日志
            $memo = '签收COD订单自动确认支付';
            app::get('ome')->model('operation_log')->write_log('order_modify@ome',$order['order_id'],$memo);
            //生成支付单
            $payment_bn = $oPayment->gen_id();
            $paymentdata = array();
            $paymentdata['payment_bn'] = $payment_bn;
            $paymentdata['order_id'] = $order['order_id'];
            $paymentdata['shop_id'] = $order['shop_id'];
            $paymentdata['currency'] = $order['currency'];
            $paymentdata['money'] = $orderData['payed'];
            $paymentdata['paycost'] = 0;
            $curr_time = time();
            $paymentdata['t_begin'] = $curr_time;//支付开始时间
            $paymentdata['t_end'] = $curr_time;//支付结束时间
            $paymentdata['cur_money'] = $orderData['payed'];
            $paymentdata['pay_type'] = 'offline';
            $paymentdata['ip'] = kernel::single("base_request")->get_remote_addr();
            $paymentdata['status'] = 'succ';
            $paymentdata['memo'] = '签收COD订单系统自动确认支付';
            $paymentdata['is_orderupdate'] = 'false';
            $oPayment->create_payments($paymentdata);
            //日志
            app::get('ome')->model('operation_log')->write_log('payment_create@ome',$paymentdata['payment_id'],'生成支付单');
        }
    }
}